#include"secondwindowNVE.h"
#include "thirdwindow.h"
#include "parameters.h"
#include<QPushButton>
#include<QDebug>
#include<QLineEdit>
#include<QLabel>
#include <vector>
#include <iostream>
#include <fstream>
#include "parameters.h"
//#include "constants.h"
#include "ljforce.h"
#include "md.h"
//#include "pimd.h"
//#include "lscivr.h"
#include "analysis.h"
#include"QDir"
#include<QFile>
#include<QTextStream>
#include<QMessageBox>
#include<QPainter>
#include<QFileDialog>
#include<QProcess>
#include<QCoreApplication>
#include<QString>
#include<QValidator>
secondwindowNVE::secondwindowNVE(QWidget *parent) : QMainWindow(parent)
{
    //setWindowState(Qt::WindowMaximized);
    this->setMinimumSize(800,600);
    this->setWindowIcon(QPixmap(":/new/Camera Roll/XH.jpeg"));
    this->setWindowTitle("分子动力学模拟");


    QLabel * labela = new QLabel("本项目模拟的是氩原子的运动，在演化轨迹时采用速度Verlet法，");
    labela ->resize(600,30);
    labela ->move(150,30);
    labela ->setParent(this);
    labela->show();

    QLabel * labelb = new QLabel("使用的势能函数是兰纳-琼斯势(LJ potential)。");
    labelb ->resize(550,30);
    labelb ->move(120,60);
    labelb ->setParent(this);
    labelb->show();

    QLabel * labelc = new QLabel("NVE");
    labelc ->resize(100,30);
    labelc ->move(50,270);
    labelc ->setParent(this);
    labelc->show();
    QFont font("Microsoft YaHei",10,75);
    labelc ->setFont(font);




    QLabel * label1 = new QLabel("温度(T/K):");
    label1 ->resize(100,30);
    label1 ->move(200,150);
    label1 ->setParent(this);
    label1->show();

    QLabel * label1_1 = new QLabel("默认值为100.0");
    label1_1->resize(150,30);
    label1_1 ->move(430,150);
    label1_1 ->setParent(this);
    label1_1->show();

    QLineEdit * line1 = new QLineEdit("100.0");
    line1 -> resize(120,30);
    line1 -> move(300,150);
    line1 ->setParent(this);
    line1 ->show();
    line1 ->setValidator(new QDoubleValidator(0.0,100.0,4,this));
  //  line1 ->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));



    QLabel * label2 = new QLabel("运行步数:");
    label2 ->resize(100,30);
    label2 ->move(210,250);
    label2 ->setParent(this);
    label2->show();

    QLabel * label2_1 = new QLabel("默认值为1000");
    label2_1->resize(150,30);
    label2_1 ->move(430,250);
    label2_1 ->setParent(this);
    label2_1->show();


    QLineEdit * line2 = new QLineEdit("1000");
    line2 -> resize(120,30);
    line2 -> move(300,250);
    line2 ->setParent(this);
    line2 ->show();
    line2 ->setValidator(new QIntValidator(this));
   // line2 ->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));
 line2 ->setValidator(new QRegularExpressionValidator(QRegularExpression("[0-9]+$")));

    QLabel * label3 = new QLabel("每");
    label3 ->resize(50,30);
    label3 ->move(270,450);
    label3 ->setParent(this);
    label3->show();
    QLabel * label3_1 = new QLabel("步输出一次");
    label3_1->resize(100,30);
    label3_1 ->move(430,450);
    label3_1 ->setParent(this);
    label3_1->show();

    QLineEdit * line3 = new QLineEdit("10");
    line3 -> resize(120,30);
    line3 -> move(300,450);
    line3 ->setParent(this);
    line3 ->show();
    line3 ->setValidator(new QIntValidator(this));
   // line3 ->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));
line3 ->setValidator(new QRegularExpressionValidator(QRegularExpression("[0-9]+$")));

    QLabel * label4 = new QLabel("时间步长(fs):");
    label4 ->resize(150,30);
    label4 ->move(170,350);
    label4 ->setParent(this);
    label4->show();
    QLineEdit * line4 = new QLineEdit("1");
    line4 -> resize(120,30);
    line4 -> move(300,350);
    line4 ->setParent(this);
    line4 ->show();
    line4 ->setValidator(new QIntValidator(this));
    //line4 ->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));
line4 ->setValidator(new QRegularExpressionValidator(QRegularExpression("[0-9]+$")));

    QLabel * label4_1 = new QLabel("默认值为1");
    label4_1->resize(100,30);
    label4_1 ->move(430,350);
    label4_1 ->setParent(this);
    label4_1->show();
     third = new thirdwindow();
//       QLineEdit * line2 = new QLineEdit;
//       line2 -> resize(200,30);
//       line2 -> move(250,200);
//       line2 ->setParent(this);

//       QLineEdit * line3 = new QLineEdit;
//       line3 -> resize(200,30);
//       line3 -> move(250,300);
//       line3 ->setParent(this);



       connect(third,&thirdwindow::thirdBack,this,[=](){
           third->close();
           this->show();
           });

     QPushButton * nextBtn = new QPushButton("运行");
     nextBtn->resize(100,40);
     nextBtn->setParent(this);
     nextBtn->move(690,550);
     nextBtn ->hide();

     QPushButton * endBtn = new QPushButton("设置完成");
     endBtn->resize(100,40);
     endBtn->setParent(this);
     endBtn->move(690,550);
     connect(endBtn,&QPushButton::clicked,[=](){
         qDebug()<<"点击了完成按钮";
         gEnsembleType = EnsembleTypes::kNve;
           gTemperature = line1->text().toDouble();
           gNsteps= line2->text().toInt();
           gNstepsWrite = line3->text().toInt();
           std::string parameter_file = "parameters.txt";
           std::ofstream ofs(parameter_file);
           if (ofs.good())
           {
               WriteParameters(parameter_file);
           }
           endBtn ->hide();
           nextBtn ->show();
        // secondwindowNVE::on_endBtn_clicked();
     });



//       QPushButton * nextBtn = new QPushButton("运行");
//       nextBtn->resize(100,40);
//       nextBtn->setParent(this);
//       nextBtn->move(690,550);
     connect(nextBtn,&QPushButton::clicked,[=](){
           qDebug()<<"点击了运行按钮";
           double a =0;
         QString curPath="";
          secondwindowNVE::on_nextBtn_clicked(a,curPath);
          QString a_str = QString::number(a, 'f');
          emit sendData(a_str,curPath);

           third->show();

       });

     connect(this,SIGNAL(sendData(QString,QString)),third,SLOT(getData(QString,QString)));
       QPushButton * backBtn = new QPushButton("返回");
       backBtn->resize(50,50);
       backBtn->setParent(this);
       backBtn->move(10,10);
       connect(backBtn,&QPushButton::clicked,[=](){
           qDebug()<<"点击了返回按钮";
           emit this-> NVEBack();
       });

}

//void secondwindowNVE::on_endBtn_clicked()
//{

//}


void secondwindowNVE::on_nextBtn_clicked(double &a,QString &curPath)
{

        std::string parameter_file = "parameters.txt";
        std::ifstream ifs(parameter_file);
        if (ifs.good())
        {
            ReadParameters(parameter_file);
        }
        // construct object of molecular system
        MolSys mol;
        // set force field
        Force *pForce = nullptr;
        if (gForceType == ForceTypes::kLennardJones)
        {
            LjForce *pLj = new LjForce(gLjSigma, gLjEpsilon);
            pForce = dynamic_cast<LjForce *>(pLj);
        }
        // start simulation: do job according to job_type
       // std::cout << "Job starts" << std::endl;
        qDebug()<<"Job starts";

        if (gDynamicsType == DynamicsTypes::kMd)
        {
            Md md;
            // initialize and evolve
            md.Initialize(mol, *pForce);
            md.Evolve(mol, *pForce);

        }

        // end simulation
      //  std::cout << "Job ends" << std::endl;
                qDebug()<<"Job ends";

       //  analysis
        std::cout << "Now computing self diffusion coefficient D ..." << std::endl;
        double diff_coef = SelfDiffusionCoefficient("out.xyz", gTimeStep);
        std::cout << "D(A^2/fs)= " << diff_coef << std::endl;
       a=diff_coef;

        // @TODO write results to GUI


 //   }
//        QDir *TEST = new QDir(QDir::currentPath());
//         bool exist = TEST->exists("Signal");
//         if(!exist)
//         {
//          TEST->mkdir("QT_Results");
//         }
//          QString fileNameGPS = "QT_Results/QT_result.csv";

//          QFile fileGPS(fileNameGPS);
//          if(!fileGPS.open(QIODevice::WriteOnly|QIODevice::Text))
//          {
//             QMessageBox::warning(this,"sdf","can't open",QMessageBox::Yes);
//          }
//          QTextStream in(&fileGPS);
   //        in << "Time(fs), T(K), Ep(kJ/mol), Ek(kJ/mol), Etotal(kJ/mol)" << endl;
//          for (int i = 0; i < ; i++){
//                 in<< timeVec[i] << "," << temperatureVec[i] << "," << EpVec[i] << "," << EkVec[i] << "," << EtotalVec[i] << endl;
//             }
    //    this->close();
    //      QDir::currentPath() = fileNameGPS;
    //      QString path=QDir::currentPath();//获取程序当前目录
    //      path.replace("/","\\");
    //      QProcess::startDetached("explorer");
       curPath = QCoreApplication::applicationDirPath();
       qDebug()<<"curPath"<<curPath;

//       QByteArray path = curPath.toLatin1();
//       char* filename = path.data();
       // QFileDialog::getOpenFileName(this,"打开文件","curPath","(*.csv)");

       this->hide();
        return ;
}

